package com.zhengtoudai.ui.presenter;

import com.zhengtoudai.global.Config;
import com.zhengtoudai.http.HttpApi;
import com.zhengtoudai.injector.PerActivity;
import com.zhengtoudai.ui.bean.Login;
import com.zhengtoudai.ui.bean.Result;
import com.zhengtoudai.ui.view.LoginView;
import com.zhengtoudai.util.DesUtil;

import javax.inject.Inject;

import rx.Subscriber;
import rx.Subscription;
import rx.functions.Action0;

/**
 * Created by Administrator on 2016/8/25 0025.
 */
@PerActivity
public class LoginPresenterImpl extends BasePresenterImpl<LoginView> implements LoginPresenter{

    @Inject
    public LoginPresenterImpl() {

    }

    @Override
    public void login(String userAccount, String userPassword) {
        try {
            userPassword = DesUtil.encrypt(userPassword, Config.DES_KEY);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Subscription mSubscription = HttpApi.getInstance().login(userAccount, userPassword)
                .doOnSubscribe(new Action0() {
                    @Override
                    public void call() {
                        mView.showDialog();
                    }
                }).subscribe(new Subscriber<Result<Login>>() {
                    @Override
                    public void onCompleted() {
                        mView.dismissDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.onFailure(e);
                        mView.dismissDialog();
                    }

                    @Override
                    public void onNext(Result<Login> result) {
                        mView.loginResult(result);
                    }
                });
        addSubscription(mSubscription);
    }


}
